<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-item label="商品图片" :labelCol="labelCol" :wrapperCol="wrapperCol">
<!--              <a-input v-decorator="['productImg']" placeholder="请粘贴商品图片地址"></a-input>-->
              <j-image-upload v-model="productImg"></j-image-upload>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="商品名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['productTitle', validatorRules.productTitle]" placeholder="请输入商品名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="商品介绍" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-textarea  v-decorator="['productInfo']" placeholder="请输入商品介绍"></a-textarea>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="商品售价(元)" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['productPrice', validatorRules.productPrice]" :min="0" placeholder="请输入商品售价" @blur="changeProductPrice"
                              style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="商品进货价(元)" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['productCostPrice', validatorRules.productCostPrice]" :min="0" @blur="changeProductPrice"
                              placeholder="请输入商品进货价" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="单个商品利润(元)" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number :precision="2" :min="0" :disabled="true" v-decorator="['productProfile']"
                              placeholder="输入售价和进货价后会自动计算" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="总库存" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number @blur="changeInventory" :min="0" v-decorator="['productAllInventory', validatorRules.productAllInventory]"
                              placeholder="请输入总库存" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="商品成交量" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number @blur="changeInventory" :min="0" v-decorator="['productVolume', validatorRules.productVolume]" placeholder="请输入商品成交量"
                              style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="剩余库存" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number :disabled="true" :min="0" v-decorator="['productRemainingInventory']"
                              placeholder="输入总库存和成交量后会自动计算" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="是否上架" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <!--              <a-input v-decorator="['productOnShelf']" placeholder="请输入是否上架，字典"></a-input>-->
              <j-dict-select-tag v-decorator="['productOnShelf', validatorRules.productOnShelf]" dict-code="pr_on_shelf" placeholder="请选择是否直接上架"
                                 :trigger-change="true"/>
            </a-form-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import JImageUpload from '@/components/jeecg/JImageUpload'
  export default {
    name: 'PrProductForm',
    components: {
      JFormContainer,
      JImageUpload
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: () => {
        },
        required: false
      },
      //表单模式：false流程表单 true普通表单
      normal: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data() {
      return {
        form: this.$form.createForm(this),
        model: {},
        productImg:[],
        disableSubmit: false,
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        confirmLoading: false,
        validatorRules: {
          productTitle: { rules: [{ required: true, message: '请输入商品名称!' }] },
          productOnShelf: { rules: [{ required: true, message: '请选择是否上架!' }] },
          productVolume: { rules: [{ required: true, message: '请输入商品成交量!' }] },
          productAllInventory: { rules: [{ required: true, message: '请输入商品总库存!' }] },
          productCostPrice: { rules: [{ required: true, message: '请输入商品进货价!' }] },
          productPrice: { rules: [{ required: true, message: '请输入商品售价!' }] },
        },
        url: {
          add: '/product/add',
          edit: '/product/edit',
          queryById: '/product/queryById'
        }
      }
    },
    watch: {},
    computed: {
      formDisabled() {
        if (this.normal === false) {
          if (this.formData.disabled === false) {
            return false
          } else {
            return true
          }
        }
        return this.disabled
      },
      showFlowSubmitButton() {
        if (this.normal === false) {
          if (this.formData.disabled === false) {
            return true
          } else {
            return false
          }
        } else {
          return false
        }
      }
    },
    created() {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData()
    },
    methods: {
      //添加
      add() {
        this.productImg=[];
        this.edit({})
      },
      //计算商品余量
      changeInventory() {
        let math = require('mathjs')
        let productAllInventory = this.form.getFieldValue('productAllInventory')
        let productVolume = this.form.getFieldValue('productVolume')
        console.log(productAllInventory, productVolume)
        if (productAllInventory && productVolume) {
          let result =math.format(math.subtract(math.bignumber(productAllInventory), math.bignumber(productVolume)));
          if (result > 0) {
            this.form.setFieldsValue({ 'productRemainingInventory': result })
          }
        }
      },
      //计算商品利润
      changeProductPrice() {
        let math = require('mathjs')
        let productPrice = this.form.getFieldValue('productPrice')
        let productCostPrice = this.form.getFieldValue('productCostPrice')
        console.log(productPrice, productCostPrice)
        if (productPrice && productCostPrice) {
          let result = math.format(math.subtract(math.bignumber(productPrice), math.bignumber(productCostPrice)));
          console.log(result)
          if (result > 0) {
            this.form.setFieldsValue({ 'productProfile': result })
          }
        }
      },
      edit(record) {
        this.form.resetFields()
        this.model = Object.assign({}, record)
        this.visible = true
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'productTitle', 'productInfo', 'productPrice', 'productCostPrice', 'productAllInventory', 'productRemainingInventory', 'productVolume', 'productProfile', 'productOnShelf'))
        })
      },
      //渲染流程表单数据
      showFlowData() {
        if (this.normal === false) {
          let params = { id: this.formData.dataId }
          getAction(this.url.queryById, params).then((res) => {
            if (res.success) {
              this.edit(res.result)
            }
          })
        }
      },
      submitForm() {
        const that = this
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true
            let httpurl = ''
            let method = ''
            if (!this.model.id) {
              httpurl += this.url.add
              method = 'post'
            } else {
              httpurl += this.url.edit
              method = 'put'
            }
            let formData = Object.assign(this.model, values)
            console.log('表单提交数据', formData)
            formData.productImg=this.productImg;
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            }).finally(() => {
              that.confirmLoading = false
            })
          }

        })
      },
      popupCallback(row) {
        this.form.setFieldsValue(pick(row,  'productTitle', 'productInfo', 'productPrice', 'productCostPrice', 'productAllInventory', 'productRemainingInventory', 'productVolume', 'productProfile', 'productOnShelf'))
      }
    }
  }
</script>